<?php
namespace app\admin\controller;
use think\Controller;
class Stats extends Controller
{


    public function staff()
    {
        $start_date = input('start_date')?input('start_date'):date('Y-m-01');
        $end_date = input('end_date')?input('end_date'):date('Y-m-d');
        $startDate = strtotime($start_date);
        $endDate = strtotime($end_date)+86400;
        $orderType = input('order_type');
        $data = db('admin')->field('name,id')->where('group_id','IN','5,2')->where('status',1)->select();
        foreach ($data as $k => $v) 
        {
            $data[$k]['lr_count'] = db('client')->where('aid',$v['id'])->whereTime('add_time','between',[$startDate,$endDate])->count();
            $data[$k]['qt_count'] = db('client')->where('xaid',$v['id'])->where('status',1)->count();
            $data[$k]['ba_count'] = db('client')->where('xaid',$v['id'])->whereTime('baoA_time','between',[$startDate,$endDate])->where('status',3)->count();
            $data[$k]['qd_count'] = db('client')->where('xaid',$v['id'])->whereTime('qiandan_time','between',[$startDate,$endDate])->where('status',4)->count();
        }
        $chartData = $data;
        if($orderType)
        {
            $orderData = array_column($data,$orderType);
            $orderData1 = array_column($chartData,$orderType);
            array_multisort($orderData,SORT_DESC,$data);
            //echarts排序问题
            array_multisort($orderData1,SORT_ASC,$chartData);
        }
        $this->assign([
            'chartData'=>$chartData,
            'data'=>$data,
            'start_date'=>$start_date,
            'end_date'=>$end_date,
            'order_type'=>$orderType
        ]);
        return view();
    }
    
    public function newStaff()
    {
        $start_date = input('start_date')?input('start_date'):date('Y-m-01');
        $end_date = input('end_date')?input('end_date'):date('Y-m-d');
        $startDate = strtotime($start_date);
        $endDate = strtotime($end_date)+86400;
        if(session('group_id','','admin')==5)
        {
            $managerId = session('uid','','admin');
            $manangerList = db('admin')->field('name,id,group_members,group_name,group_color')->where('group_id','IN','5')->where('status',1)->where('id',$managerId)->select();
        }
        else if (session('group_id','','admin')==1)
        {
            $manangerList = db('admin')->field('name,id,group_members,group_name,group_color')->where('group_id','IN','5')->where('status',1)->select();
        }

//----------------------------------------------------------------------------------------------------------------------------------------------------------------
/*        //根据组 遍历出 所有组员 查询 client 表 获取 每个组员的数据
        foreach($manangerList as $key => $val){
            $manangerList[$key]['group_members'] = $val['id'] . ',' . $val['group_members'];
        }

        $member_list_arr = [];
        foreach($manangerList as $key => $val){
            $member_list_arr[$key] = db('admin')->field('name,id')->where('id','IN',$val['group_members'])->where('status',1)->select();
        }

        $data = [];
        foreach($member_list_arr as $key => $val){
            foreach($val as $k => $v){
                $data[$key][$k]['name'] = $v['name'];
                $data[$key][$k]['lr_count'] = db('client')->where('aid',$v['id'])->whereTime('add_time','between',[$startDate,$endDate])->count();
                $data[$key][$k]['qt_count'] = db('client')->where('xaid',$v['id'])->where('status',1)->count();
                $data[$key][$k]['ba_count'] = db('client')->where('xaid',$v['id'])->whereTime('baoA_time','between',[$startDate,$endDate])->where('status',3)->count();
                $data[$key][$k]['qd_count'] = db('client')->where('xaid',$v['id'])->whereTime('qiandan_time','between',[$startDate,$endDate])->where('status',4)->count();
            }
        }

        foreach($manangerList as $key => $val){
            foreach($data as $k => $v){
                if($key == $k){
                    $data[$k]['group_name'] = $val['group_name'];
                }
            }
        }*/
//----------------------------------------------------------------------------------------------------------------------------------------------------------------

        $data = [];
        $i = 0;
        foreach ($manangerList as $k => $v)
        {
            $data[$i]['name'] = $v['group_name'];
            $i++;
            $data[$i]['name'] = $v['name'];
            $data[$i]['lr_count'] = db('client')->where('aid',$v['id'])->whereTime('add_time','between',[$startDate,$endDate])->count();
            $data[$i]['qt_count'] = db('client')->where('xaid',$v['id'])->where('status',1)->count();
            $data[$i]['ba_count'] = db('client')->where('xaid',$v['id'])->whereTime('baoA_time','between',[$startDate,$endDate])->where('status',3)->count();
            $data[$i]['qd_count'] = db('client')->where('xaid',$v['id'])->whereTime('qiandan_time','between',[$startDate,$endDate])->where('status',4)->count();
            $i++;
            $groupMemberList = db('admin')->field('name,id')->where('id','IN',$v['group_members'])->where('status',1)->select();

            foreach ($groupMemberList as $kk => $vv)
            {
                $data[$i]['name'] = $vv['name'];
                $data[$i]['lr_count'] = db('client')->where('aid',$vv['id'])->whereTime('add_time','between',[$startDate,$endDate])->count();
                $data[$i]['qt_count'] = db('client')->where('xaid',$vv['id'])->where('status',1)->count();
                $data[$i]['ba_count'] = db('client')->where('xaid',$vv['id'])->whereTime('baoA_time','between',[$startDate,$endDate])->where('status',3)->count();
                $data[$i]['qd_count'] = db('client')->where('xaid',$vv['id'])->whereTime('qiandan_time','between',[$startDate,$endDate])->where('status',4)->count();
                $i++;
            }
        }
//----------------------------------------------------------------------------------------------------------------------------------------------------------------

        $chartData = array_reverse($data);

        if($orderType)
        {
            $orderData = array_column($data,$orderType);
            $orderData1 = array_column($chartData,$orderType);
            array_multisort($orderData,SORT_DESC,$data);
            //echarts排序问题
            array_multisort($orderData1,SORT_ASC,$chartData);
        }

//        echo "<pre>";
//        print_r($chartData);
//        echo "<br>";
//        print_r($data);
//        die;

        $this->assign([
            'chartData'=>$chartData,
            'data'=>$data,
            'start_date'=>$start_date,
            'end_date'=>$end_date,
            'order_type'=>$orderType
        ]);
        return view();
    }

    public function manager()
    {
        if(session('group_id','','admin')==5 && !input('manager_id'))
        {
            //$_SERVER['QUERY_STRING']?header("Location: {$_SERVER['REQUEST_URI']}&manager_id=".session('uid','','admin')):header("Location: ?manager_id=".session('uid','','admin'));
        }
        $start_date = input('start_date')?input('start_date'):date('Y-m-01');
        $end_date = input('end_date')?input('end_date'):date('Y-m-d');
        $startDate = strtotime($start_date);
        $endDate = strtotime($end_date)+86400;
        $orderType = input('order_type');
        $managerId = input('manager_id');//session('group_id','','admin')==5?session('uid','','admin'):
        $managers = db('admin')->field('name,id,group_members,group_name')->where('group_id','IN','5')->where('status',1)->select();
        if($managerId)
        {
            $manager = db('admin')->field('name,id,group_members,group_name')->where('id',$managerId)->find();
            $groupMembers = $manager['group_members']?$manager['id'].','.$manager['group_members']:$manager['id'];
            $data = db('admin')->field('name,id,group_members')->where('id','IN',$groupMembers)->where('status',1)->order('group_id DESC,id ASC')->select();
            foreach ($data as $k => $v) 
            {
                $data[$k]['lr_count'] = db('client')->where('aid',$v['id'])->whereTime('add_time','between',[$startDate,$endDate])->count();
                $data[$k]['qt_count'] = db('client')->where('xaid',$v['id'])->where('status',1)->count();
                $data[$k]['ba_count'] = db('client')->where('xaid',$v['id'])->whereTime('baoA_time','between',[$startDate,$endDate])->where('status',3)->count();
                $data[$k]['qd_count'] = db('client')->where('xaid',$v['id'])->whereTime('qiandan_time','between',[$startDate,$endDate])->where('status',4)->count();
            }
            if(input('hide_manage') == 1 )
            {
                unset($data[0]);
            }
        }
        else
        {
            $data = $managers;
            foreach ($data as $k => $v) 
            {
                $group_members = $v['group_members']?$v['id'].','.$v['group_members']:$v['id'];
                $data[$k]['lr_count'] = db('client')->where('aid','IN',$group_members)->whereTime('add_time','between',[$startDate,$endDate])->count();
                $data[$k]['qt_count'] = db('client')->where('xaid','IN',$group_members)->where('status',1)->count();
                $data[$k]['ba_count'] = db('client')->where('xaid','IN',$group_members)->whereTime('baoA_time','between',[$startDate,$endDate])->where('status',3)->count();
                $data[$k]['qd_count'] = db('client')->where('xaid','IN',$group_members)->whereTime('qiandan_time','between',[$startDate,$endDate])->where('status',4)->count();
            }

        }

        $chartData = $data;
        if($orderType)
        {
            $orderData = array_column($data,$orderType);
            $orderData1 = array_column($chartData,$orderType);
            array_multisort($orderData,SORT_DESC,$data);
            //echarts排序问题
            array_multisort($orderData1,SORT_ASC,$chartData);
        }
        $this->assign([
            'managers'=>$managers,
            'manager'=>$manager,
            'chartData'=>$chartData,
            'data'=>$data,
            'manager_id'=>$managerId,
            'start_date'=>$start_date,
            'end_date'=>$end_date,
            'order_type'=>$orderType
        ]);
        if($managerId)
            return view('manager_');
        else
            return view();
    }
    
    public function departData()
    {
        if(input('limit'))
        {
            if(session('group_id','','admin')==5)
            {
                $managerId = session('uid','','admin');
                $manangerList = db('admin')->field('name,id,group_members,group_name,group_color,max_count_day')->where('group_id','IN','5')->where('status',1)->where('id',$managerId)->select();
            }
            else if (session('group_id','','admin')==1)
            {
                $manangerList = db('admin')->field('name,id,group_members,group_name,group_color,max_count_day')->where('group_id','IN','5')->where('status',1)->select();
            }
            
            $data = [];
            $i = 0;
            foreach ($manangerList as $k => $v)
            {
                $ii = $i;
                $data[$ii]['name'] = '【'.$v['group_name'].'】';
                $i++;
                $data[$i]['name'] = $v['name'];
                $data[$i]['max_count_day'] = $v['max_count_day'];
                $data[$ii]['day_lr'] += $data[$i]['day_lr'] = db('client')->where('aid',$v['id'])->whereTime('add_time','today')->count();
                $data[$ii]['week_lr'] += $data[$i]['week_lr'] = db('client')->where('aid',$v['id'])->whereTime('add_time','week')->count();
                $data[$ii]['week_qd'] += $data[$i]['week_qd'] = db('client')->where('aid',$v['id'])->where('status','4')->whereTime('add_time','week')->count();
                $data[$ii]['month_lr'] += $data[$i]['month_lr'] = db('client')->where('aid',$v['id'])->whereTime('add_time','month')->count();
                $data[$ii]['month_qd'] += $data[$i]['month_qd'] = db('client')->where('aid',$v['id'])->where('status','4')->whereTime('add_time','month')->count();
                $data[$ii]['total_lr'] += $data[$i]['total_lr'] = db('client')->where('aid',$v['id'])->count();
                $data[$ii]['total_qd'] += $data[$i]['total_qd'] = db('client')->where('aid',$v['id'])->where('status','4')->count();
                $i++;
                $groupMemberList = db('admin')->field('name,id,max_count_day')->where('id','IN',$v['group_members'])->where('status',1)->select();
                foreach ($groupMemberList as $kk => $vv)
                {
                    $data[$i]['name'] = $vv['name'];
                    $data[$i]['max_count_day'] = $vv['max_count_day'];
                    $data[$ii]['day_lr'] += $data[$i]['day_lr'] = db('client')->where('aid',$vv['id'])->whereTime('add_time','today')->count();
                    $data[$ii]['week_lr'] += $data[$i]['week_lr'] = db('client')->where('aid',$vv['id'])->whereTime('add_time','week')->count();
                    $data[$ii]['week_qd'] += $data[$i]['week_qd'] = db('client')->where('aid',$vv['id'])->where('status','4')->whereTime('add_time','week')->count();
                    $data[$ii]['month_lr'] += $data[$i]['month_lr'] = db('client')->where('aid',$vv['id'])->whereTime('add_time','month')->count();
                    $data[$ii]['month_qd'] += $data[$i]['month_qd'] = db('client')->where('aid',$vv['id'])->where('status','4')->whereTime('add_time','month')->count();
                    $data[$ii]['total_lr'] += $data[$i]['total_lr'] = db('client')->where('aid',$vv['id'])->count();
                    $data[$ii]['total_qd'] += $data[$i]['total_qd'] = db('client')->where('aid',$vv['id'])->where('status','4')->count();
                    $i++;
                }
            }
            return ['code'=>0,'count'=>count($data),'data'=>$data,'msg'=>''];
        }else
            return view();
    }

    public function staffData()
    {
        return view();


    }
    
    public function expendType()
    {
        if(input('limit'))
        {
            $typeList = ['雪鲸','可赞','东创','星权','徐涛','徐操'];
            $data['live'] = '';
        }
        else
            return view();
    }
    

    public function operatorsAndGoodsData()
    {
        if(input('limit'))
        {
            $operators = db('admin')->where('group_id',7)->where('status',1)->orderRaw('CONVERT(name USING gbk)')->select();
            $goods = db('goods')->where('is_del',0)->order('brand ASC, id ASC')->select();
            $startDate = input('start_date')?date('Y-m-d',input('start_date')):date('Y-m-01');
            $endDate = input('end_date')?date('Y-m-t',input('end_date')):date('Y-m-t');
            foreach ($operators as $k=>$v)
            {
                $operators[$k]['count'] = db('active_page_log')->field('sum(single_count) as single_count , sum(topic_count) as topic_count ')->where('admin_id',$v['id'])->whereTime('complete_time','between',[$startDate,$endDate])->find();
                $operators[$k]['count_'] = db('active_page_log')->where('admin_id',$v['id'])->whereTime('complete_time','between',[$startDate,$endDate])->where('single_count',0)->where('topic_count',0)->count();
            }
            
            foreach ($goods as $k=>$v)
            {
                
            }
        }
        else
        {
            $data['start_date'] = input('start_date')?date('Y-m-d',input('start_date')):date('Y-m-01');
            $data['end_date'] = input('end_date')?date('Y-m-t',input('end_date')):date('Y-m-t');
            $this->assign(['data'=>$data]);
            return view();
        }
            
    }


}
